/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iddaa.core.util;

import java.util.Date;

/**
 *
 * @author aziz
 */
public class UniqueId {

    public String generate(String key) throws Exception {
        return byteArrayToHexString(computeHash(key+new Date().toString()));
    }

    private byte[] computeHash(String x) throws Exception {
        java.security.MessageDigest d = null;
        d = java.security.MessageDigest.getInstance("md5");
        d.reset();
        d.update(x.getBytes());
        return d.digest();
    }

    private String byteArrayToHexString(byte[] b) {
        StringBuffer sb = new StringBuffer(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            int v = b[i] & 0xff;
            if (v < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString().toUpperCase();
    }
}
